// 初始化默认选中一个菜单
$(function(){
	var objs = $("#smoothmenu1 a[isSelected='true']");
	var obj;
	var imgPath = "img/menu/";
	for(var i = 0; i < objs.length; i++) {
		obj = objs.get(i);
		if(obj.level == "1") {
			$(obj).css("background","url('" + imgPath + "menu_over.jpg')");
			$(obj).css("color", "black");
			$(obj).attr("selected","true");
		}else if(obj.level == "2") {
			$(obj).css("background","url('" + imgPath + "submenu_over.jpg')");
			$(obj).css("color","black");
			$(obj).attr("selected","true");
			}
		}
		
		menuAutoSize();
	});
   
	$(window).bind("resize",function(){
	menuAutoSize();
});


// 菜单项显示状态
var leftFirstVisiable = -1;// 左侧第一个显示的菜单索引
var leftLastHide = -1;// 左侧最后一个隐藏的菜单索引
var rightFirstHide = -1; // 右侧第一个隐藏的菜单索引
var rightLastVisiable = -1;// 右侧最后一个显示的菜单索引
   
	// 根据页面宽度调整菜单显示
function menuAutoSize(){
	   var totalWidth = document.body.clientWidth;
	   var menuItemWidth = 120;
	   var menuLeftWidth = 185;
	   var menuRightWidth = 95;
	   
	   // 可见菜单数
	   var visiableCount = (totalWidth - menuLeftWidth - menuRightWidth) / menuItemWidth;
	   visiableCount = parseInt(visiableCount);
	   
	   // 总菜单数
	   var menuItemObjs = $("#smoothmenu1 a[level='1']");
	   var totalCount = menuItemObjs.length;
	   
	   for(var i = 0; i < visiableCount; i++){
		   menuItemObjs[i].style.display = ""; 
	   }
	   
	   var top_menu_right = document.getElementById('top_menu_right');
	   if(totalCount > visiableCount){// 有需要隐藏菜单
		   // 显示图标
		   top_menu_right.style.display = "";
		   top_menu_right.style.left = totalWidth - 90;
		   
		   // 隐藏菜单
		   for(var i = visiableCount ; i < totalCount; i++){
			   disableBack();
			   enableForward();
			   menuItemObjs[i].style.display = "none";
		   }
		   leftFirstVisiable = 0;// 左侧第一个显示的菜单索引
		   leftLastHide = -1;// 左侧最后一个隐藏的菜单索引
		   rightFirstHide = visiableCount; // 右侧第一个隐藏的菜单索引
		   rightLastVisiable = visiableCount - 1;// 右侧最后一个显示的菜单索引
	   }else {
		   top_menu_right.style.display = "none";
	   }
	   
}

// 前进
function menuForward(){
	var menuItemObjs = $("#smoothmenu1 a[level='1']");
	if(rightFirstHide == -1 || 
	  rightFirstHide >= menuItemObjs.length){// 右侧没有隐藏菜单
		disableForward();
		return;
	}
	
	menuItemObjs[rightFirstHide].style.display = "";
	menuItemObjs[leftFirstVisiable].style.display = "none";
	
	leftFirstVisiable++;
	leftLastHide++;
	rightFirstHide++;
	rightLastVisiable++; 
	
	enableBack();
	if(rightFirstHide == -1 || 
	  rightFirstHide >= menuItemObjs.length){// 右侧没有隐藏菜单
		disableForward();
	}
}

// 后退
function menuBack(){
	var menuItemObjs = $("#smoothmenu1 a[level='1']");
	if(leftLastHide == -1){// 左侧没有隐藏菜单
		disableBack();
		return;
	}
	
	menuItemObjs[leftLastHide].style.display = "";
	menuItemObjs[rightLastVisiable].style.display = "none";
	
	leftFirstVisiable--;
	leftLastHide--;
	rightFirstHide--;
	rightLastVisiable--;
	
	enableForward();
	
	if(leftLastHide == -1){// 左侧没有隐藏菜单
		disableBack();
	}
}

function enableBack(){
	$('#menuBack').attr('flag', 'enable');
	$('#menuBack').css('filter','alpha(opacity=100)');
}

function disableBack(){
	$('#menuBack').attr('flag', 'disable');
	$('#menuBack').css('filter','alpha(opacity=50)');
}

function enableForward(){
	$('#menuForward').attr('flag', 'enable');
	$('#menuForward').css('filter','alpha(opacity=100)');
}

function disableForward(){
	$('#menuForward').attr('flag', 'disable');
	$('#menuForward').css('filter','alpha(opacity=50)');
}

// 鼠标移入图片
function imgOver(imgObj) {
	if(imgObj.flag == 'disable') {
		return;
	}
	$(imgObj).css('filter','alpha(opacity=50)'); 
};

// 鼠标移出图片
function imgOut(imgObj){
	if(imgObj.flag == 'disable') {
		return;
	}
	$(imgObj).css('filter','alpha(opacity=100)'); 
};
